/*
	Anonymous Image Board Image Downloader - program for downloading pictures from AIB
	Copyright (C) 2011-2012 Metallhead <Metallhead@megabox.ru>

	HomePage:    http://code.google.com/p/aibid/

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QFileDialog>
#include <QSettings>
#include <QMessageBox>



settingsDialog::settingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::settingsDialog)
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
    ui->setupUi(this);
	//ui->manualUpdate->setVisible(false);
    ui->path->setText(settings.value("settings/path").toString());
    ui->timeInterval->setText(settings.value("settings/time").toString());
    ui->comboBox->setCurrentIndex(settings.value("settings/language").toInt());
    ui->sameCheck->setChecked(settings.value("settings/hashCheck").toBool());
    startNum = settings.value("settings/language").toInt();
    startTime = settings.value("settings/time").toString().toInt();
	ui->enableTray->setChecked(settings.value("settings/tray").toBool());
	//ui->manualUpdate->setChecked(settings.value("settings/manualUpdate").toBool());
	ui->rememberThreads->setChecked(settings.value("settings/rememberThreads").toBool());
	ui->threadsLimit->setText(settings.value("settings/threadsLimit").toString());
	Common::fillParsersCombobox(ui->detectors);
	currentParser = ui->detectors->currentText();

	languagesList << "en" << "ru";

	ui->exitAct->setEnabled(ui->enableTray->isChecked());
	ui->exitAct->setCurrentIndex(settings.value("settings/close").toBool());
}

settingsDialog::~settingsDialog()
{
    delete ui;
}

void settingsDialog::on_buttonBox_accepted()
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	settings.setValue("settings/path",ui->path->text());
	settings.setValue("settings/time",ui->timeInterval->text());
	settings.setValue("settings/language",ui->comboBox->currentIndex());
	settings.setValue("settings/currentLanguage",languagesList.at(ui->comboBox->currentIndex()));
	settings.setValue("settings/hashCheck",ui->sameCheck->isChecked());
	settings.setValue("settings/tray",ui->enableTray->isChecked());
	//settings.setValue("settings/manualUpdate",ui->manualUpdate->isChecked());
	settings.setValue("settings/rememberThreads",ui->rememberThreads->isChecked());
	settings.setValue("settings/threadsLimit",ui->threadsLimit->text());
	if(startNum!=ui->comboBox->currentIndex())
	{
		QMessageBox * mes = new QMessageBox(this);
		mes->setWindowTitle("Warning");
		mes->setText("Please restart the application to apply the changes.");
		mes->exec();
	}
	if(currentParser!=ui->detectors->currentText())
	{
		settings.setValue("settings/parser",ui->detectors->currentText());
		detectors = Common::readDetectors(ui->detectors->currentText());
		currentParser = ui->detectors->currentText();
	}
	timeChanged=startTime!=ui->timeInterval->text().toInt();

	if(!ui->rememberThreads->isChecked())
		emit saveThreadsOff();
	else
		emit saveThreadsOn();


	if(ui->enableTray->isChecked())
		settings.setValue("settings/close",bool(ui->exitAct->currentIndex()));
	else
		settings.setValue("settings/close",false);

	this->close();
}

void settingsDialog::on_setPath_clicked()
{
	QString tempPath = ui->path->text();
    ui->path->setText(QFileDialog::getExistingDirectory(this, tr("Open Directory"),ui->path->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
	if(ui->path->text()=="")
		ui->path->setText(tempPath);
}

void settingsDialog::on_enableTray_clicked()
{
	if(ui->enableTray->isChecked())
	{
		tray->show();
		ui->exitAct->setEnabled(true);
	}
	else
	{
		tray->hide();
		ui->exitAct->setEnabled(false);
	}
}

void settingsDialog::on_buttonBox_rejected()
{
	if(trayShowed && (!tray->isVisible()))
		ui->enableTray->setChecked(true);
	if((!trayShowed) && tray->isVisible())
		ui->enableTray->setChecked(false);
	on_enableTray_clicked();
	this->close();
}
void settingsDialog::checkInput()
{
	 QRegExp reg1("[0-9]+");
     QRegExp reg2("[0-9]+");
     ui->buttonBox->buttons().at(0)->setEnabled(reg1.exactMatch(ui->timeInterval->text())&&reg2.exactMatch(ui->threadsLimit->text()));
}

void settingsDialog::on_timeInterval_textChanged(QString)
{
    checkInput();
}

void settingsDialog::on_threadsLimit_textChanged(QString)
{
    checkInput();
}

void settingsDialog::showEvent(QShowEvent *)
{
	trayShowed = ui->enableTray->isChecked();
	Common::fillParsersCombobox(ui->detectors);
}
